package com.ztgf.commodity.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.commodity.common.config.YmlConfig;
import com.ztgf.commodity.mapper.CommodityApplyAgentMapper;
import com.ztgf.commodity.models.bo.CommodityApplyAgentBO;
import com.ztgf.commodity.models.bo.CommodityApplyAgentSaveBO;
import com.ztgf.commodity.models.dto.CommodityApplyAgentDTO;
import com.ztgf.commodity.models.dto.CommodityApplyAgentLogDTO;
import com.ztgf.commodity.models.service.ICommodityApplyAgentLogService;
import com.ztgf.commodity.models.service.ICommodityApplyAgentService;
import com.ztgf.commodity.models.vo.CommodityApplyAgentLogVO;
import com.ztgf.commodity.models.vo.CommodityApplyAgentVO;
import com.ztgf.commodity.models.vo.CommoditySysUserVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.List;

import static com.ztgf.common.utils.UtilConstants.*;

/**
 * 商品代理申请表控制器
 *
 * @author chenyu.wang
 * @Date 2020-06-23 17:07:09
 */
@SuppressWarnings("AlibabaCommentsMustBeJavadocFormat")
@DubboService
public class CommodityApplyAgentServiceImpl extends ServiceImpl<CommodityApplyAgentMapper, CommodityApplyAgentDTO> implements ICommodityApplyAgentService {

    @Autowired
    private ICommodityApplyAgentLogService commodityApplyAgentLogService;

    @Resource
    private YmlConfig ymlConfig;

    @Override
    public boolean add(CommodityApplyAgentSaveBO saveBO) {
        CommodityApplyAgentDTO dto = new CommodityApplyAgentDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(ymlConfig.getPkId());
        boolean insert = this.insert(dto);
        if (!insert) {
            return false;
        }
        CommodityApplyAgentLogDTO logDTO = new CommodityApplyAgentLogDTO();
        logDTO.setApplyAgentId(dto.getId());
        logDTO.setDealContent("发起了代理申请!");
        commodityApplyAgentLogService.add(logDTO);
        return true;
    }


    /**
     * @description:后台管理系统代理申请列表
     * @author: chenyu.wang
     * @date: 2020-09-11
     * @return:
     */
    @SuppressWarnings("AlibabaCommentsMustBeJavadocFormat")
    @Override
    public Page<CommodityApplyAgentVO> queryPageList(CommodityApplyAgentBO bo) {
        Page<CommodityApplyAgentVO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        List<CommodityApplyAgentVO> list = baseMapper.queryPageList(page, bo);
        page.setRecords(list);
        return page;
    }

    /**
     * @description:指定经办人
     * @author: chenyu.wang
     * @date: 2020-09-11
     * @return:
     */
    @Override
    public boolean updateManagerUser(Long id, Long managerUserId) {
        CommodityApplyAgentDTO dto = this.selectById(id);
        if (dto == null) {
            return false;
        }
        CommodityApplyAgentDTO updateDTO = new CommodityApplyAgentDTO();
        updateDTO.setId(dto.getId());
        updateDTO.setManagerUserId(managerUserId);
        boolean update = this.updateById(updateDTO);
        if (!update) {
            return false;
        }
        CommodityApplyAgentLogDTO logDTO = new CommodityApplyAgentLogDTO();
        logDTO.setApplyAgentId(dto.getId());
        logDTO.setManagerUserId(managerUserId);
        logDTO.setDealState(dto.getDealState());
        logDTO.setDealContent("重新指定了经办人!");
        commodityApplyAgentLogService.add(logDTO);
        return true;
    }

    /**
     * @description:代理申请列表更新操作,0不改状态，1更改状态
     * @author: chenyu.wang
     * @date: 2020-09-11
     * @return:
     */
    @Override
    public boolean updateState(Long id, Integer state, String content, Long managerUserId) {
        CommodityApplyAgentDTO dto = this.selectById(id);
        if (dto == null) {
            return false;
        }
        if (state == ZERO) {
            CommodityApplyAgentDTO updateDTO = new CommodityApplyAgentDTO();
            updateDTO.setId(dto.getId());
            updateDTO.setDealContent(content);
            updateDTO.setDealState(ONE);
            updateDTO.setUpdateUserId(managerUserId);
            boolean update = this.updateById(updateDTO);
            if (!update) {
                return false;
            }
            CommodityApplyAgentLogDTO logDTO = new CommodityApplyAgentLogDTO();
            logDTO.setApplyAgentId(dto.getId());
            logDTO.setManagerUserId(managerUserId);
            logDTO.setDealState(ONE);
            logDTO.setDealContent(content);
            commodityApplyAgentLogService.add(logDTO);
            return true;
        } else {
            CommodityApplyAgentDTO updateDTO = new CommodityApplyAgentDTO();
            updateDTO.setId(dto.getId());
            updateDTO.setDealContent(content);
            updateDTO.setDealState(TWO);
            updateDTO.setUpdateUserId(managerUserId);
            boolean update = this.updateById(updateDTO);
            if (!update) {
                return false;
            }
            CommodityApplyAgentLogDTO logDTO = new CommodityApplyAgentLogDTO();
            logDTO.setApplyAgentId(dto.getId());
            logDTO.setManagerUserId(managerUserId);
            logDTO.setDealState(TWO);
            logDTO.setDealContent(content);
            commodityApplyAgentLogService.add(logDTO);
            return true;
        }
    }

    /**
     * @description:查询申请日志
     * @author: chenyu.wang
     * @date: 2020-09-11
     * @return:
     */
    @Override
    public List<CommodityApplyAgentLogVO> queryLog(Long id) {
        return baseMapper.queryLog(id);
    }

    /**
     * @description:查询系统经办人
     * @author: chenyu.wang
     * @date: 2020-09-11
     * @return:
     */
    @Override
    public List<CommoditySysUserVO> queryManagerUser() {
        return baseMapper.queryManagerUser();
    }


}
